/*
 * Copyright 2014 - 2017 Henning Dodenhof
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.android.baseconfig.common.views;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Path;
import android.util.AttributeSet;
import android.util.TypedValue;

import com.android.baseconfig.R;

import androidx.appcompat.widget.AppCompatImageView;


public class RoundImageView extends AppCompatImageView {

    private float width, height;
    private boolean onlyDrawUp = false;

    public RoundImageView(Context context) {
        this(context, null);
    }

    public RoundImageView(Context context, int defultRadiusDp) {
        this(context, null);
        this.radius = dip2px(context, defultRadiusDp);
    }

    public RoundImageView(Context context, float defultRadius) {
        this(context, null);
        this.radius = defultRadius;
    }

    public RoundImageView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public RoundImageView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        getAttribute(context, attrs, defStyleAttr);
    }

    private float radius = 12;

    private void getAttribute(Context context, AttributeSet attrs, int defStyleAttr) {
        //获取自定义属性。
        TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.RoundImageView);
        if (typedArray != null) {
            radius = typedArray.getDimensionPixelSize(R.styleable.RoundImageView_round_img_radius, context
                    .getResources().getDimensionPixelSize(R.dimen.dp_10));;
            onlyDrawUp = typedArray.getBoolean(R.styleable.RoundImageView_round_img_only_up, false);;
            typedArray.recycle();
            return;
        }
    }

    @Override
    protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
        super.onLayout(changed, left, top, right, bottom);
        width = getWidth();
        height = getHeight();
    }

    @Override
    protected void onDraw(Canvas canvas) {

        if (width > radius && height > radius) {
            Path path = new Path();
            path.moveTo(radius, 0);
            path.lineTo(width - radius, 0);
            path.quadTo(width, 0, width, radius);

            if (onlyDrawUp) {
                path.lineTo(width, height);
                path.lineTo(0, height);
                path.lineTo(0, radius);
                path.quadTo(0, 0, radius, 0);
            } else {
                path.lineTo(width, height - radius);
                path.quadTo(width, height, width - radius, height);
                path.lineTo(radius, height);
                path.quadTo(0, height, 0, height - radius);
                path.lineTo(0, radius);
                path.quadTo(0, 0, radius, 0);

            }
            canvas.clipPath(path);
        }

        super.onDraw(canvas);
    }

    public void setRadius(float radiusPx) {
        this.radius = radius;
        this.requestLayout();
    }

    public void setOnlyUp(boolean onlyDrawUp){
        this.onlyDrawUp=onlyDrawUp;
    }

    private int dip2px(Context context, int dp) {
        float pxDimension = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dp, context.getResources().getDisplayMetrics());
        return (int) pxDimension;
    }

}